package cst.a703xmq.startproject_703xmq;

import android.app.ProgressDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import cst.a703xmq.startproject_703xmq.Bean.WeatherInfo;
import cst.a703xmq.startproject_703xmq.Listener.WeatherListener;
import cst.a703xmq.startproject_703xmq.Model.WeatherModel;
import cst.a703xmq.startproject_703xmq.Model.WeatherModel51;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, WeatherListener{

    private EditText cityNOInput;
    private TextView city;
    private TextView cityNO;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    private TextView sd;
    private ProgressDialog loadingWeather;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cityNOInput = (EditText) findViewById(R.id.et_city_no);
        city = (TextView) findViewById(R.id.tv_city);
        cityNO = (TextView) findViewById(R.id.tv_city_no);
        temp = (TextView) findViewById(R.id.tv_temp);
        wd = (TextView) findViewById(R.id.tv_wd);
        ws = (TextView) findViewById(R.id.tv_ws);
        sd = (TextView) findViewById(R.id.tv_sd);

        loadingWeather = new ProgressDialog(this);
        loadingWeather.setTitle("查询中……");

        findViewById(R.id.button).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);
    }
    @Override
    public void onClick(View view)
    {
        loadingWeather.show();
        if(view.getId()==R.id.button)
        {
            WeatherModel weathermodel =new WeatherModel();
            weathermodel.getWeather(cityNOInput.getText().toString(), this);
        }
        else if(view.getId()==R.id.button2)
        {

            WeatherModel51 weathermodel51 =new WeatherModel51();
            weathermodel51.getWeather(cityNOInput.getText().toString(), this);
        }
    }
    @Override
    public void onResponse(WeatherInfo weatherinfo)
    {
        loadingWeather.dismiss();
        if (weatherinfo != null) {
            city.setText(weatherinfo.getCity());
            cityNO.setText(weatherinfo.getCityid());
            temp.setText(weatherinfo.getTemp());
            wd.setText(weatherinfo.getWD());
            ws.setText(weatherinfo.getWS());
            sd.setText(weatherinfo.getSD());
        } else {
            city.setText("未知");
        }
    }
    @Override
    public void onFail(String msg)
    {
        loadingWeather.dismiss();
        city.setText(msg);
    }
}
